package com.itheima.Test;

import jdk.nashorn.internal.codegen.CompilerConstants;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;

import java.util.HashMap;
import java.util.concurrent.*;

/**
 * @PackageName: com.itheima.Test
 * @ClassName: callableTest
 * @Author: Renyz
 * @Date: 2021/11/14 15:12
 * @Description: //TODO
 */


public class callableTest implements Callable<String> {

    @Override
    public String call() throws Exception {
        return Thread.currentThread().getName();
    }
}

class callableDemo{
    public static void main(String[] args) {
        CopyOnWriteArrayList<FutureTask> futureTasks = new CopyOnWriteArrayList<>();
        callableTest callableTest = new callableTest();

        for (int i = 0; i < 15; i++) {
            FutureTask<String> stringFutureTask = new FutureTask<>(callableTest);
            futureTasks.add(stringFutureTask);
        }
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 5, 2L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(2)
        ,Executors.defaultThreadFactory(),new ThreadPoolExecutor.CallerRunsPolicy());
        for (FutureTask futureTask : futureTasks) {
            threadPoolExecutor.submit(futureTask);
        }



//        Thread aaa = new Thread(stringFutureTask, "aaa");
//
//        aaa.start();
//
//        String s = null;
//        try {
//            s = stringFutureTask.get();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//        System.out.println(s);

        for (FutureTask futureTask : futureTasks) {
            try {
                System.out.println(futureTask.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        threadPoolExecutor.shutdown();


    }
}